package com.acme.bankapp.domain.bank;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

public class Bank implements Serializable {
	private static final long serialVersionUID = -5820680544388350360L;

	private Collection<Client> clients;
	
	public Bank() {
		clients = new ArrayList<>();
	}
	
	public Collection<Client> getClients() {
		return clients;
	}
	
	public void addClient(Client c) {
		clients.add(c);
	}
	
	public void saveBank(File f) throws FileNotFoundException, IOException {
		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
			oos.writeObject(clients);
		}
	}
	
	@SuppressWarnings("unchecked")
	public Bank loadBank(File f) throws FileNotFoundException, IOException, ClassNotFoundException {
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
			clients.addAll((Collection<? extends Client>)ois.readObject());
		}
		return this;
		
	}
}
